#include <olectl.h>
#include "IPEditUI.h"
#define SetadAll(Code) {for(int i = 0; i < 7; ++i){if ( GetItemAt(i)){if (i%2 == 0){((CIPEditUI*)GetItemAt(i))->Code;}else{((CLabelUI*)GetItemAt(i))->Code;}}}}


CIPEditUI::CIPEditUI() : 
	m_pNextEdit(NULL),m_pFrontEdit(NULL),m_bInRange(true)
{
	SetTextPadding(CDuiRect(4, 3, 4, 3));
	SetBkColor(0xFFFFFFFF);
	SetTextStyle(5);

}

LPCTSTR CIPEditUI::GetClass() const
{
	return _T("IPEditUI");
}

LPVOID CIPEditUI::GetInterface(LPCTSTR pstrName)
{
	if( _tcscmp(pstrName, DUI_CTR_EDIT) == 0 ) 
	{
		return static_cast<CIPEditUI*>(this);
	}
	return CEditUI::GetInterface(pstrName);
}


void CIPEditUI::DoEvent(TEventUI& event)
{

	if (event.Type == UIEVENT_CHAR)
	{
		OnChar(event);
	}

	CEditUI::DoEvent(event);
}


void CIPEditUI::OnChar(TEventUI& event)
{
	HWND hWnd = m_pWindow->GetHWND();
	WPARAM wParam = event.wParam;
	if ( !((wParam >= '0'&& wParam <= '9') || wParam ==VK_BACK ||wParam == VK_LEFT ||wParam == VK_RIGHT) )
	{
		return ;
		//bHandled = FALSE;
	}
	if (wParam == '.')
	{
		if (m_bInRange)
		{
			if ( !(HIWORD(Edit_GetSel(hWnd))-LOWORD(Edit_GetSel(hWnd))) &&Edit_GetTextLength(hWnd) > 0 )
			{
				ToNext();
			}

		}
		if ( LOWORD(Edit_GetSel(hWnd)) == Edit_GetTextLength(hWnd))  
		{
			m_bInRange = true;
			ToNext();
		}

		
	}
	else if (wParam == VK_BACK)
	{
		if (LOWORD(Edit_GetSel(hWnd)) == 0)
		{
			ToNext(true);
		}
	}

	else if (Edit_GetTextLength(hWnd) > 1)// && !(HIWORD(Edit_GetSel(m_hWnd))-LOWORD(Edit_GetSel(m_hWnd)))  )
	{
		TCHAR szText[64];
		Edit_GetText(hWnd,szText,sizeof(szText)/sizeof(TCHAR));
		if (Edit_GetTextLength(hWnd) == 2 || (Edit_GetTextLength(hWnd) == 3 && HIWORD(Edit_GetSel(hWnd))-LOWORD(Edit_GetSel(hWnd))))
		{
			szText[LOWORD(Edit_GetSel(hWnd))] = wParam;
			szText[3] = 0;
		}

		//if (HIWORD(Edit_GetSel(m_hWnd))-LOWORD(Edit_GetSel(m_hWnd)))
		//{
		//	szText[LOWORD(Edit_GetSel(m_hWnd))] = wParam;
		//	szText[LOWORD(Edit_GetSel(m_hWnd)) + 1] = szText [HIWORD(Edit_GetSel(m_hWnd)) + 1];
		//	szText[LOWORD(Edit_GetSel(m_hWnd)) + 2] = szText [HIWORD(Edit_GetSel(m_hWnd)) + 2];
		//}
			
		if (_tstoi(szText) >= 255 )
		{
			Edit_SetText(hWnd ,_T("255"));
			m_bInRange = false;
		}
		else 
		{
			m_bInRange = true;
			if (Edit_GetTextLength(hWnd)== 2&&LOWORD(Edit_GetSel(hWnd)) == 2 || (Edit_GetTextLength(hWnd)== 3&&LOWORD(Edit_GetSel(hWnd)) == 3))
			{
				ToNext();
			}
		}
	
	}
	
}

void CIPEditUI::ToNext( bool bReverse)
{
	if (bReverse)
	{
		if(m_pFrontEdit)
		{
			m_pFrontEdit->SetFocus();
			TCHAR szWord[64];
			TCHAR* szText = szWord+5;
			_tcscpy_s(szText ,59,m_pFrontEdit->GetText());
			int nLen = _tcslen(szText) ;
			//szText[nLen] = 0;
			//m_pOwner->m_pFrontEdit->SetText(szText);
			m_pFrontEdit->SetSel(nLen ,nLen);
		}

	}
	else
	{
		if (m_bInRange && m_pNextEdit)
		{
			m_pNextEdit->SetFocus();
			m_pNextEdit->SetSelAll();
		}
	}
}


void CIPEditUI::SetNextEdit(CIPEditUI* pEdit)
{
	m_pNextEdit = pEdit;
	pEdit->SetFrontEdit(this);
}

void CIPEditUI::SetFrontEdit(CIPEditUI* pEdit)
{
	m_pFrontEdit = pEdit;
}

///////////////////////////////////////////////
//
//
///////////////////////////////////////////////


CIPAddress::CIPAddress()
	:m_nWidth(0)
	,m_nHeight(0)
{
	InitEditItem();
	
}

void CIPAddress::InitEditItem()
{
	CContainerUI::RemoveAll();
	RECT rcLabel={0};
	m_pItem1 = new CIPEditUI;
	CContainerUI::Add(m_pItem1);
	rcLabel = ResetItem(m_pItem1,0);
	CLabelUI* pTemp = new CLabelUI;
	CContainerUI::Add(pTemp);
	pTemp->SetFont(1);
	pTemp->SetText(_T("."));
	pTemp->SetFloat(true);
	pTemp->SetPos(rcLabel);
	

	m_pItem2 = new CIPEditUI;
	CContainerUI::Add(m_pItem2);
	rcLabel = ResetItem(m_pItem2,1);
	pTemp = new CLabelUI;
	CContainerUI::Add(pTemp);
	pTemp->SetFont(1);
	pTemp->SetText(_T("."));
	pTemp->SetFloat(true);
	pTemp->SetPos(rcLabel);
	

	m_pItem3 = new CIPEditUI;
	CContainerUI::Add(m_pItem3);
	rcLabel = ResetItem(m_pItem3,2);
	pTemp = new CLabelUI;
	CContainerUI::Add(pTemp);
	pTemp->SetFont(1);
	pTemp->SetText(_T("."));
	pTemp->SetFloat(true);
	pTemp->SetPos(rcLabel);
	

	m_pItem4 = new CIPEditUI;
	CContainerUI::Add(m_pItem4);
	ResetItem(m_pItem4,3);


	m_pItem1->SetNextEdit(m_pItem2);
	m_pItem2->SetNextEdit(m_pItem3);
	m_pItem3->SetNextEdit(m_pItem4);
}

void CIPAddress::AdjustPos()
{
	RECT rcLabel;
	CIPEditUI* pEdit = NULL;
	CLabelUI* pLabel = NULL;
	for(int i = 0 ,j=0; i < 4  ;++i)
	{
		pEdit = (CIPEditUI*)CContainerUI::GetItemAt(i*2);
		if (pEdit)
		{
			rcLabel = ResetItem(pEdit ,i);
		}
		if (i < 3)
		{
			pLabel = (CLabelUI*)CContainerUI::GetItemAt(i*2 + 1);
			if (pLabel)
			{
				pLabel->SetPos(rcLabel);
			}
		}
	}
}

LPVOID CIPAddress::GetInterface()
{
	return this;
}

LPCTSTR CIPAddress::GetClass()const
{
	return _T("IPAddress");
}

CDuiString CIPAddress::GetIPText()const
{
	TCHAR szIP[64];
	_tcscpy_s(szIP,sizeof(szIP)/sizeof(TCHAR),m_pItem1->GetText());
	_tcscat_s(szIP,sizeof(szIP)/sizeof(TCHAR),_T("."));
	_tcscat_s(szIP,sizeof(szIP)/sizeof(TCHAR),m_pItem2->GetText());
	_tcscat_s(szIP,sizeof(szIP)/sizeof(TCHAR),_T("."));
	_tcscat_s(szIP,sizeof(szIP)/sizeof(TCHAR),m_pItem3->GetText());
	_tcscat_s(szIP,sizeof(szIP)/sizeof(TCHAR),_T("."));
	_tcscat_s(szIP,sizeof(szIP)/sizeof(TCHAR),m_pItem4->GetText());
	return szIP;
}

UINT CIPAddress::GetIP()const
{
	UINT nIP = -1;
	nIP &= _tcstoul(m_pItem1->GetText(),NULL ,10) >> 24;
	nIP &= _tcstoul(m_pItem2->GetText(),NULL,10) >> 16;
	nIP &= _tcstoul(m_pItem3->GetText(),NULL,10) >> 8;
	nIP &= _tcstoul(m_pItem4->GetText(),NULL,10);
	return nIP;
}

void CIPAddress::SetIPText(UINT nIP)
{
	TCHAR szIP[64];
	_stprintf_s(szIP,sizeof(szIP)/sizeof(TCHAR) ,_T("%d"), (nIP<<24) & 0xFF);
	m_pItem1->SetText(szIP);
	_stprintf_s(szIP,sizeof(szIP)/sizeof(TCHAR) ,_T("%d"), (nIP<<16) & 0xFF);
	m_pItem2->SetText(szIP);
	_stprintf_s(szIP,sizeof(szIP)/sizeof(TCHAR) ,_T("%d"), (nIP<<8) & 0xFF);
	m_pItem3->SetText(szIP);
	_stprintf_s(szIP,sizeof(szIP)/sizeof(TCHAR) ,_T("%d"), nIP & 0xFF);
	m_pItem4->SetText(szIP);

}

void CIPAddress::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
{
	if(_tcscmp(pstrName , _T("bkcolor") ) == 0)
	{
        while( *pstrValue > _T('\0') && *pstrValue <= _T(' ') ) 
		{
			pstrValue = ::CharNext(pstrValue);
		}
        if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
        LPTSTR pstr = NULL;
        DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
        SetBkColor(clrColor);
	}
	else if (_tcscmp(pstrName ,_T("inset")) == 0);
	else if (_tcscmp(pstrName ,_T("pos")) == 0)
	{
        RECT rcPos = { 0 };
        LPTSTR pstr = NULL;
        rcPos.left = _tcstol(pstrValue, &pstr, 10);  ASSERT(pstr);    
        rcPos.top = _tcstol(pstr + 1, &pstr, 10);    ASSERT(pstr);    
        rcPos.right = _tcstol(pstr + 1, &pstr, 10);  ASSERT(pstr);    
        rcPos.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);    
        SIZE szXY = {rcPos.left >= 0 ? rcPos.left : rcPos.right, rcPos.top >= 0 ? rcPos.top : rcPos.bottom};
        SetFixedXY(szXY);
        SetFixedWidth(rcPos.right - rcPos.left);
        SetFixedHeight(rcPos.bottom - rcPos.top);
	}
	else 
	{
		CContainerUI::SetAttribute(pstrName ,pstrValue);
	}
}
void CIPAddress::SetPos(RECT rcPos)
{
	CContainerUI::SetPos(rcPos);
	AdjustPos();
}

//void CIPAddress::SetPadding(RECT rcPadding)
//{
//	CContainerUI::SetPadding(rcPadding);
//	AdjustPos();
//}
void CIPAddress::SetBkColor(DWORD dwColor)
{
	CContainerUI::SetBkColor(dwColor);
	m_pItem1->SetBkColor(dwColor);
	m_pItem1->SetNativeEditBkColor(dwColor);
	m_pItem2->SetBkColor(dwColor);
	m_pItem2->SetNativeEditBkColor(dwColor);
	m_pItem3->SetBkColor(dwColor);
	m_pItem3->SetNativeEditBkColor(dwColor);
	m_pItem4->SetBkColor(dwColor);
	m_pItem4->SetNativeEditBkColor(dwColor);

}

RECT CIPAddress::ResetItem(CIPEditUI* pItem ,int nIndex)
{
	RECT rcPanel = CContainerUI::GetPos();

	int nWidth = rcPanel.right - rcPanel.left;
	int nItem = 0;
	if (nWidth > 0)
	{
		nItem = (nWidth - DotWidth*3)/4;
	}
	rcPanel.left += nIndex* nItem + nIndex* DotWidth ;
	rcPanel.right = rcPanel.left + nItem;
	pItem->SetFloat(true);
	pItem->SetPos(rcPanel);
	rcPanel.left = rcPanel.right ;
	rcPanel.right = rcPanel.left + DotWidth;
	return rcPanel;
}

//
//void CIPAddress::SetFixedWidth(int cx)
//{
//
//	CContainerUI::SetFixedWidth(cx);
//	AdjustPos();
//}
//
//
//void CIPAddress::SetFixedHeight(int cy)
//{
//	CContainerUI::SetFixedHeight( cy);
//	AdjustPos();
//}
//
//void CIPAddress::DoEvent(TEventUI& event)
//{
//	CContainerUI::DoEvent(event);
//}



